package com.snj.twelve;

import java.util.regex.Pattern;

public class EmailValidatorWithRegex {
    // 定义邮箱的正则表达式
    private static final String EMAIL_REGEX = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
    private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);

    public static boolean isValidEmail(String email) {
        if (email == null) {
            return false;
        }
        // 利用正则模式匹配传入的邮箱
        return EMAIL_PATTERN.matcher(email).matches();
    }

    public static void main(String[] args) {
        String validEmail = "x@1.com";
        String invalidEmail = "example.example.com";
        System.out.println(validEmail + " 是否有效: " + isValidEmail(validEmail));
        System.out.println(invalidEmail + " 是否有效: " + isValidEmail(invalidEmail));
    }
}